Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience methods to read the value of float and int literals #148

Closed
wants to merge 1 commit into from

Conversation

ahoppen
Copy link
Collaborator

@ahoppen ahoppen commented Oct 3, 2019

I was trying to get the integer/float value of integer/float literals and was surprised to find there is no method for it and you need to parse the value from the characters in the digits token yourself. I feel like there should be a convenience method for that.

If you disagree and don't think we should increase the API surface for these kinds of methods, I‘m also happy with the decision.

@ahoppen
Copy link
Collaborator Author

ahoppen commented Oct 3, 2019

@swift-ci Please test

Copy link
Contributor

@harlanhaskins harlanhaskins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these.

@ahoppen
Copy link
Collaborator Author

ahoppen commented Oct 3, 2019

@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Oct 3, 2019

Build failed
Swift Test OS X Platform
Git Sha - af07b60

@ahoppen
Copy link
Collaborator Author

ahoppen commented Oct 4, 2019

@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Oct 4, 2019

Build failed
Swift Test OS X Platform
Git Sha - 9b587ee

@rintaro
Copy link
Member

rintaro commented Oct 5, 2019

Could you handle number literals with _? Currently Double/Int.init(_:String) returns nil for strings like "1_000".

@ahoppen
Copy link
Collaborator Author

ahoppen commented Oct 7, 2019

@rintaro Ah. I didn’t think about that. Looking into the compiler we’d also need to support

  1. Hexadecimal int literals
  2. Octal int literals
  3. Binary int literals
  4. Exponential float literals (e.g. 5e+2)
  5. Hexadecimal float literals with possible exponents (e.g. 0x5.fp+2)

1 - 3 should be fairly easy to implement using Int(_:radix:) but Swift is currently lacking corresponding initializers for 4 and 5. While I believe it would still be useful to have properties that are able to parse these (I’m sure nearly no developer will think about case 5), it’s out of scope for now.

I filed SR-11580 to keep track of the issue.

@ahoppen ahoppen closed this Oct 7, 2019
@ahoppen ahoppen deleted the literal-value branch January 14, 2023 08:27
adevress pushed a commit to adevress/swift-syntax that referenced this pull request Jan 14, 2024
Don't apply `FileprivateAtFileScope` rule to extensions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants